<template>
  <!--头部区域-->
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>教材管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--页面主体-->
    <el-container>
      <!---->
      <el-aside width="180px">
        <!--侧边栏菜单区域-->
        <el-menu
          :router="true"
          :default-active="activePath"
          active-text-color="#409EFF"
          :unique-opened="true"
          background-color="#D3DCE6"
          class="el-menu-vertical-demo">
          <!--一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id" >
            <!--一级菜单的模板区域-->
            <template slot="title">
              <!--图标-->
              <i :class="iconsObj[item.id]"></i>
              <!--文本-->
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children"
              :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                <template slot="title">
                  <!--图标-->
                  <i :class="iconsObj[item.id]"></i>
                  <!--文本-->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右边内容主体-->
      <el-main>
        <!--路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data () {
    return {
      menuList: [
        {
          id: 1,
          authName: '学院信息',
          children: [
            {id: 11, authName: '学院列表', path: 'campus'}
          ]
        },
        {
          id: 2,
          authName: '教师管理',
          children: [
            {id: 21, authName: '教师信息', path: 'teachers'}
          ]
        },
        {
          id: 3,
          authName: '教材管理',
          children: [
            {id: 31, authName: '教材信息', path: 'bookManagement'}
          ]
        },
        {
          id: 4,
          authName: '数据统计',
          children: [
            {id: 41, authName: '图表统计', path: 'statistics'}
          ]
        }
      ],
      iconsObj: {
        1: 'iconfont icon-icon-test35',
        2: 'iconfont icon-icon-test16',
        3: 'iconfont icon-icon-test11',
        4: 'iconfont icon-icon-test9',
        5: 'iconfont icon-icon-test31',
        6: 'iconfont icon-icon-test29',
        7: 'iconfont icon-icon-test13'
      },
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    // this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    // this.information.$on('activePath', path => {
    //     this.activePath = path
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const {data: res} = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    //  保存链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>

<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
  .el-header {
    background-color: #B3C0D1;
    color: black;
    font-size: 20px;
    text-align: center;
    /*line-height: 60px;*/
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    > div {
      display: flex;
      align-items: center;
      margin-left: 15px;
      span {
        margin-left: 15px;
      }
    }
  }
  .el-header img{
    height: 50px;
    width: 50px;
  }

  .el-submenu .el-menu-item {
    padding: 0 10px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    /*text-align: center;*/
    /*line-height: 200px;*/
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    /*text-align: center;*/
    /*line-height: 160px;*/
  }

  .iconfont {
    margin-right: 10px;
    margin-left: 10px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

</style>
